package com.ofek.eventos;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.ofek.applicationbase.utils.Log;
import com.ofek.eventos.db.datatypes.EventData;

public class EventsFragment extends ListFragment {
	public interface EventsFragmentController {
		public void onShowing(Fragment frag);
		public void onEventPressed(EventData event);
	}
	public EventListAdapter listAdapter;
	private View progress;
	private ArrayList<EventData> eventsToShow;
	private boolean editState;
	private boolean waiting;
	private View empty;

	public static final EventsFragment newInstance() {
		final EventsFragment f = new EventsFragment();
		final Bundle bdl = new Bundle(3);
		f.setArguments(bdl);
		return f;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		Log.d("onCreate");
		super.onCreate(savedInstanceState);
		setWaitingState(true);
	}


	@Override
	public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
			final Bundle savedInstanceState) {
		final View inflate = inflater.inflate(R.layout.diary_layout, container,false);
		progress = inflate.findViewById(R.id.progress);
		progress.setVisibility(waiting ? View.VISIBLE : View.GONE);
		empty = inflate.findViewById(R.id.empty);
		empty.setVisibility(View.GONE);
		empty.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
				final EventsFragmentActivity activity = (EventsFragmentActivity) EventsFragment.this.getActivity();
				activity.onAddClicked();
			}
		});
		return inflate;
	}
	@Override
	public void onActivityCreated(final Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);


	}
	@Override
	public void onListItemClick(final ListView l, final View v, final int position, final long id) {
		final EventsFragmentController activity = (EventsFragmentController) EventsFragment.this.getActivity();
		final EventData event=listAdapter.getItem(position);
		activity.onEventPressed(event);
	}

	@Override
	public void onResume() {
		super.onResume();
		final EventsFragmentController activity = (EventsFragmentController)getActivity();
		activity.onShowing(this);
	}

	public void setWaitingState(final boolean waiting) {
		this.waiting = waiting;
		if ( progress!=null ) 
			progress.setVisibility(waiting ? View.VISIBLE : View.GONE);
	}

	public void setEventsToShow(final ArrayList<EventData> events) {
		eventsToShow=events;
		setWaitingState(false);
		if (eventsToShow==null) return; 
		listAdapter = new EventListAdapter(getActivity(),events);
		getListView().setAdapter(listAdapter);
		listAdapter.setEditState(editState);
		empty.setVisibility(eventsToShow.size()<=0 ? View.VISIBLE : View.GONE);
	}

	public void setEdit(final boolean edit) {	
		editState = edit;
		if (isResumed()) {
			listAdapter.setEditState(edit);
		}
	}

	public void setEmptyStringRes(final int res) {
		((TextView) empty).setText(res);
	}
}